import 'package:flutter/material.dart';

class NewRouteDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("New route"),
      ),
      body: Column(
        children: <Widget>[
          //点击该按钮后弹出对话框
          RaisedButton(
            child: Text("对话框1"),
            onPressed: () async {
              //弹出对话框并等待其关闭
              bool delete = await showDeleteConfirmDialog1(context);
              if (delete == null) {
                print("取消删除");
              } else {
                print("已确认删除");
                //... 删除文件
              }
            },
          ),
          RaisedButton(
            child: Text("对话框1"),
            onPressed: () async {
              int i = await showDialog<int>(
                  context: context,
                  builder: (BuildContext context) {
                    return SimpleDialog(
                      title: const Text('请选择语言'),
                      children: <Widget>[
                        SimpleDialogOption(
                          onPressed: () {
                            // 返回1
                            Navigator.pop(context, 1);
                          },
                          child: Padding(
                            padding: const EdgeInsets.symmetric(vertical: 6),
                            child: const Text('中文简体'),
                          ),
                        ),
                        SimpleDialogOption(
                          onPressed: () {
                            // 返回2
                            Navigator.pop(context, 2);
                          },
                          child: Padding(
                            padding: const EdgeInsets.symmetric(vertical: 6),
                            child: const Text('美国英语'),
                          ),
                        ),
                      ],
                    );
                  });

              if (i != null) {
                print("选择了：${i == 1 ? "中文简体" : "美国英语"}");
              }
            },
          ),

          RaisedButton(
            child: Text("对话框3"),
            onPressed: () async {

              int index = await showDialog<int>(
                context: context,
                builder: (BuildContext context) {
                  var child = Column(
                    children: <Widget>[
                      ListTile(title: Text("请选择")),
                      Expanded(
                          child: ListView.builder(
                            itemCount: 30,
                            itemBuilder: (BuildContext context, int index) {
                              return ListTile(
                                title: Text("$index"),
                                onTap: () => Navigator.of(context).pop(index),
                              );
                            },
                          )),
                    ],
                  );
                  //使用AlertDialog会报错
                  //return AlertDialog(content: child);
                  return Dialog(child: child);
                },
              );
              if (index != null) {
                print("点击了：$index");
              }


            },
          ),


        ],
      ),
    );
  }
}

// 弹出对话框
Future<bool> showDeleteConfirmDialog1(BuildContext context) {
  return showDialog<bool>(
    builder: (context) {
      return AlertDialog(
        title: Text("提示"),
        content: Text("您确定要删除当前文件吗?"),
        actions: <Widget>[
          FlatButton(
            child: Text("取消"),
            onPressed: () => Navigator.of(context).pop(), // 关闭对话框
          ),
          FlatButton(
            child: Text("删除"),
            onPressed: () {
              //关闭对话框并返回true
              Navigator.of(context).pop(true);
            },
          ),
        ],
      );
    },
    context: context,
  );
}
